﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtletIS.Service.Helpers
{
    public static class TimeSpanHelper
    {
        /// <summary>
        ///  Konvertuje stringový čas v stotinách na TimeSpan
        /// </summary>
        public static TimeSpan FromString(string strTime)
        {
            int hours, minutes, seconds, hundreds;
            strTime = strTime.PadLeft(8, '0');

            hours = Convert.ToInt32(strTime.Substring(0, 2));
            minutes = Convert.ToInt32(strTime.Substring(2, 2));
            seconds = Convert.ToInt32(strTime.Substring(4, 2));
            hundreds = Convert.ToInt32(strTime.Substring(6, 2));

            return new TimeSpan(0, hours, minutes, seconds, hundreds * 10);
        }

        /// <summary>
        ///  Konvertuje string tvaru 12:00 alebo 12:00:00 na TimeSpan
        /// </summary>
        public static TimeSpan FromDbString(string sTime)
        {
            switch (sTime.Length)
            {
                case 5:
                    return new TimeSpan(Convert.ToInt32(sTime.Substring(0, 2)), Convert.ToInt32(sTime.Substring(3, 2)), 0);

                case 8:
                    return new TimeSpan(Convert.ToInt32(sTime.Substring(0, 2)), Convert.ToInt32(sTime.Substring(3, 2)), Convert.ToInt32(sTime.Substring(6, 2)));

                default:
                    return new TimeSpan(0, 0, 0);
            }
        }

        /// <summary>
        ///  Konvertuje TimeSpan na stringový čas v stotinách
        /// </summary>
        public static string ToResultString(TimeSpan ts)
        {
            return string.Format("{0}{1:00}{2:00}{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        }





    }
}
